/*
 * ImportSources module for Netbeans.
 *
 * Copyright (c) 2007, Rogue
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * For queries or bugs, you may contact me via e-mail: roguexz [at] gmail [dot] com
 */
package rogue.netbeans.module.importsources.ui;

import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.event.ChangeListener;
import rogue.netbeans.module.importsources.*;

/**
 * UI representation of the WizardPanel2 class.
 *
 * @author Rogue
 */
public final class WizardPanel2UI extends JPanel implements StatusNotifier {
    
    /**
     * Creates new form WizardPanel2UI
     */
    public WizardPanel2UI(ChangeListener lsnr) {
        initComponents();
        scanProgressBar.addChangeListener(lsnr);
    }
    
    public void scanFilesInTheBackground(String rootDir, String[] fileExtensions) {
        // clear the previous data.
        jTree1 = new JTree(new Object[]{"Scanning for sources..."});
        jScrollPane1.setViewportView(jTree1);
        this.rootDir = rootDir;
        
        scanFiles = new ScanFiles(rootDir, fileExtensions, this);
        
        scanProgressBar.setMinimum(0);
        scanProgressBar.setMaximum(100);
        scanProgressBar.setIndeterminate(true);
        
        // start the scan.
        Thread t = new Thread(new Runnable() {
            public void run() {
                beginScanning();
            }
        });
        t.start();
    }
    
    public JTree getTree() {
        return jTree1;
    }
    
    public void updateStatus(String message) {
        statusLabel.setText(message);
    }
    
    public void cancelScan() {
        if(!scanFiles.isScanComplete()) {
            scanFiles.terminateScan();
        }
    }
    
    public String getName() {
        return "Select the files to be imported.";
    }
    
    private void beginScanning() {
        JCheckBoxTreeNode node = scanFiles.getSelectedFiles();
        
        // specify that the scan is complete.
        scanProgressBar.setIndeterminate(false);
        scanProgressBar.setValue(100);
        
        // change the strings.
        scanProgressBar.setVisible(false);
        
        if(node!=null) {
            jTree1 = new JCheckBoxTree(node);
            jScrollPane1.setViewportView(jTree1);
            
            displayLabel.setText("<html>Completed scanning the selected directory.<br/>Now select the files that you would like to import.</html>");
            return;
        }
        displayLabel.setText("Error occured. The returned node was null!");
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        scanProgressBar = new javax.swing.JProgressBar();
        displayLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new JTree(new Object[]{"Scanning for sources..."});
        statusLabel = new javax.swing.JLabel();

        scanProgressBar.setFocusable(false);

        org.openide.awt.Mnemonics.setLocalizedText(displayLabel, "Please wait while the wizard scans the specified directory for source files.");
        displayLabel.setFocusable(false);

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setViewportView(jTree1);

        org.openide.awt.Mnemonics.setLocalizedText(statusLabel, " ");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, scanProgressBar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, displayLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, statusLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 384, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(displayLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(scanProgressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(statusLabel))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel displayLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree jTree1;
    private javax.swing.JProgressBar scanProgressBar;
    private javax.swing.JLabel statusLabel;
    // End of variables declaration//GEN-END:variables
    private boolean scanComplete = false;
    private volatile boolean terminateScan = true;
    private ScanFiles scanFiles = null;
    private String rootDir = null;
}

